<template>
  <div>
    <h1>商品分类</h1>
    <div>
      <el-form :inline="true" :model="searchForm" class="demo-form-inline">
        <el-form-item label="类型名称">
          <el-input
            v-model="searchForm.keyWord"
            placeholder="请输入"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            v-model="searchForm.typeStatus"
            placeholder="请选择"
            clearable
            style="width: 150px"
          >
            <el-option label="显示" value="0" />
            <el-option label="隐藏" value="1" />
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="loadTypeTree()">查询</el-button>
        <el-button type="primary" @click="onAddRootType">新增类型</el-button>
      </el-form>
    </div>
    <div>
      <el-table
        :data="tableData"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        default-expand-all
      >
        <el-table-column prop="typeId" label="编号" />
        <el-table-column prop="typeName" label="类型名称" />
        <el-table-column prop="typeStatus" label="状态">
          <template #default="scope">
            <span v-if="scope.row.typeStatus == 0">显示</span>
            <span v-if="scope.row.typeStatus == 1">隐藏</span>
          </template>
        </el-table-column>
        <el-table-column prop="typeImage" label="图片">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <el-image
                :src="scope.row.typeImage"
                style="width: 50px; height: 50px"
              />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="typeTop" label="排序" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="primary" @click="onAddType(scope.row)" size="small"
              >新增</el-button
            >
            <el-button
              type="success"
              @click="onEditType(scope.row)"
              size="small"
              >编辑</el-button
            >
            <el-button
              type="danger"
              @click="onDeleteType(scope.row)"
              size="small"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-dialog v-model="dialogFormVisible" title="新增分类" width="500">
        <el-form :model="form" ref="ruleFormRef" :rules="rules">
          <el-form-item
            label="类型名称"
            :label-width="formLabelWidth"
            prop="typeName"
          >
            <el-input v-model="form.typeName" autocomplete="off" />
          </el-form-item>
          <el-form-item
            label="状态"
            :label-width="formLabelWidth"
            prop="typeStatus"
          >
            <el-radio-group v-model="form.typeStatus">
              <el-radio :value="0">显示</el-radio>
              <el-radio :value="1">隐藏</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="分类排名"
            :label-width="formLabelWidth"
            prop="typeTop"
          >
            <el-input v-model="form.typeTop" autocomplete="off" />
          </el-form-item>
          <el-form-item label="图片" :label-width="formLabelWidth">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:10010/prod/type/uploadImages"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
            </el-upload>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="dialogFormVisible = false">重置</el-button>
            <el-button type="primary" @click="submitForm(ruleFormRef)">
              提交
            </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from "vue";
import { getTypeTree, addType } from "../api/api";

import { ElMessage, type FormInstance, type FormRules } from "element-plus";

import { Plus } from "@element-plus/icons-vue";

import type { UploadProps } from "element-plus";

const imageUrl = ref("");

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  imageUrl.value = response.data;
  form.value.typeImage = imageUrl.value;
};
const ruleFormRef = ref<FormInstance>();

const dialogFormVisible = ref(false);
const formLabelWidth = "140px";

const searchForm = ref({
  keyWord: "",
  typeStatus: "",
});
const form = ref({
  typeId: null,
  typeImage: "",
  typeName: null,
  typePid: null,
  typeStatus: null,
  typeTop: null,
});
interface RuleForm {
  typeName: string;
  typeStatus: string;
  typeTop: string;
}
const rules = ref<FormRules<RuleForm>>({
  typeName: [{ required: true, message: "名称不能为空", trigger: "blur" }],
  typeStatus: [{ required: true, message: "状态不能为空", trigger: "blur" }],
  typeTop: [{ required: true, message: "排序不能为空", trigger: "blur" }],
});

const tableData = ref([]);

const loadTypeTree = () => {
  getTypeTree(searchForm.value).then((res) => {
    if (res.data.code == 200) {
      tableData.value = res.data.data;
    }
  });
};

const onAddRootType = () => {
  form.value = {
    typeId: null,
    typeImage: "",
    typeName: null,
    typePid: 0,
    typeStatus: null,
    typeTop: null,
  };
  dialogFormVisible.value = true;
};
const onAddType = (row: any) => {
  console.log("类型子列表添加", row);
  form.value = {
    typeId: null,
    typeImage: "",
    typeName: null,
    typePid: row.id,
    typeStatus: null,
    typeTop: null,
  };
  dialogFormVisible.value = true;
};

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      form.value.typeImage = imageUrl.value;
      addType(form.value).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("添加成功");
          loadTypeTree();
          dialogFormVisible.value = false;
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    } else {
      console.log("error submit!", fields);
    }
  });
};

onMounted(() => {
  loadTypeTree();
});
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
